<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
+
<script>
  export default {
    name: 'app',
    components: {
    },
    data() {
      return {
      }
    },
    methods: {
      //读取token
      loadToken: function () {
        let token = this.$cookies.get('Authorization');
        console.log(token);
        if (token) {
          this.$axios.defaults.headers.common['Authorization'] = token;
        } else {
          this.$axios.defaults.headers.common['Authorization'] = '';
        }
      },
      jumpLogin: function () {
        let token = this.$cookies.get('Authorization');
        if (token) {

        } else {
          this.$router.push('/login');
        }
      }
    },
    created() {
      this.loadToken();
      //监听路由变化
      this.$router.beforeEach((to, from, next) => {

        //to:即将前往的页面,from:从哪个页面来,next:下一步操作
        console.log(233333);
        console.log(to);
        console.log(from);
        console.log(next);

        if (to.path == '/login') {
          next();
        } else {
          let token = this.$cookies.get('Authorization');
          console.log(token)
          if (token) {
            next();
          } else {
            next({ path: '/login' })
          }
        }
      })
    },
    mounted() {
      this.loadToken();
      this.jumpLogin();
    }
  }
</script>

<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
    /*margin-top: 60px;*/
  }
</style>